<?php
	global $SSO, $Security, $AppID, $User, $DB;
	//Check to see if there is a valid session 
	$SessionInfo = $SSO->isValidSession("index.php?action=login", $SSO->getSessionInformation());	
		
	//Check to see if the user has rights to be on LivingWeb
	$PageRights = array('ADMIN', 'USER');
	$UserRoles = $Security->ValidateUser($SessionInfo['username'], $AppID, $PageRights, 1);
	
	//Grab the user info
	$UserInfo = $User->GetUserInfo($SessionInfo['username']);	

	$array_of_divs = explode(",",$UserInfo['div_codes']);
	if(trim($UserInfo['div_codes']) == "" && !in_array("ADMIN",$UserRoles))
	{
		print '<script>alert(\'You do not have access to any ministries.\nContact your adminsitrator if you believe this to be an error.\');history.back(-1);</script><noscript><span class="T1">You do not have access to any ministries.  Contact your adminsitrator if you believe this to be an error.</span></noscript>';
	}
	elseif(!in_array($_GET['div_code'],$array_of_divs) && !in_array("ADMIN",$UserRoles))
	{
		print '<script>alert(\'You do not have access to this ministry.\nContact your adminsitrator if you believe this to be an error.\');history.back(-1);</script><noscript><span class="T1">You do not have access to this page.  Contact your adminsitrator if you believe this to be an error.</span></noscript>';
	}
	else
	{
		global $DB;
		if(isset($_GET['div_code']))
		{
			$DB->query("SELECT MinistryName FROM ministries WHERE MinistryID = '" .  $_GET['div_code'] . "'");
			$row = $DB->fetch_row();
		}
		if(isset($_GET['link_num']) && isset($_GET['div_code']))
		{
			$DB->query("DELETE FROM menu WHERE LinkOrder = '" . $_GET['link_num'] . "' AND MinistryID = '" . $_GET['div_code'] . "'");
		}
	}
?>
<script src="/CommonClientScripts/XMLRequestHandler.js" type="text/javascript"></script>
<script language="javascript1.2" type="text/javascript">
	function buildURL(pid) {
		return 'index.php?PID=' + pid;
	}
		
	var PROCESSID;
		
	function showHideEdit(linkNum) {	
		var elementId = 'Bar' + linkNum;
		var divHandle = document.getElementById(elementId);

		if(divHandle.style.display == 'none')
		{		
			divHandle.style.display = 'block';
			divHandle.style.height = '';
			increaseSize(elementId,linkNum,100);
			PROCESSID = window.setInterval( "increaseSize('"+elementId+"','"+linkNum+"',100)", 200 );
		}
		else
		{
			var elementId = 'Content' + linkNum;
			var divHandle = document.getElementById(elementId);
			divHandle.style.display = 'none';

			var elementId = 'Bar' + linkNum;
			PROCESSID = window.setInterval( "decreaseSize('"+elementId+"','"+linkNum+"',10)", 10 );
		}
	}
	
  function increaseSize(elementId, linkNum, maxSize) {
		var divHandle = document.getElementById(elementId);
		var numSize = divHandle.style.height.slice(0,divHandle.style.height.length-2);
				
		if(numSize<=maxSize)
			divHandle.style.height = numSize + 1 + "px";		
		else
		{
			window.clearInterval(PROCESSID);
			var divHandle = document.getElementById("Content" + linkNum);
			divHandle.style.display = 'block';
			document.forms['Form'+linkNum].txtDisplay.focus();			
			getXMLMenuItem(linkNum);
		}
	}

  function decreaseSize(elementId, linkNum, minSize) {
		var divHandle = document.getElementById(elementId);
		var numSize = divHandle.style.height.slice(0,divHandle.style.height.length-2);
		
		if(numSize>minSize)
			divHandle.style.height = numSize - 10 + "px";		
		else
		{
			var elementId = 'Bar' + linkNum;
			var divHandle = document.getElementById(elementId);
			divHandle.style.display = 'none';
			window.clearInterval(PROCESSID);
		}
	}

	function getXMLMenuItem(linkNum) {
		var oXMLRequestHandler = new XMLRequestHandler();
		var paramArray = new Array(linkNum);
		
		oXMLRequestHandler.LoadUrl("http://" + location.host + "/LivingWeb/get_xml_menu_item.php?div_code="+ getURLParam('div_code') +"&link_order="+linkNum, populateForm, paramArray);
	}

	function populateForm(oXMLResponse, linkNum) {
		if(linkNum != 'New') {
			var oNode = oXMLResponse.SelectSingleNode( "/dataset/record/column[@name='Display']" ) ;	
			document.forms['Form'+linkNum].txtDisplay.value = oNode.attributes.getNamedItem('value').value ;
	
			//var oNode = oXMLResponse.SelectSingleNode( "/dataset/record/column[@name='LinkOrder']" ) ;	
			//document.forms['Form'+linkNum].txtOrder.value = oNode.attributes.getNamedItem('value').value ;
	
			var oNode = oXMLResponse.SelectSingleNode( "/dataset/record/column[@name='Link']" ) ;	
			document.forms['Form'+linkNum].txtLink.value = oNode.attributes.getNamedItem('value').value ;
		}
	}
	/*
		getURLParam()
		http://www.11tmr.com/11tmr.nsf/d6plinks/MWHE-695L9Z
	*/
	function getURLParam(strParamName){
		var strReturn = "";
		var strHref = window.location.href;
		if ( strHref.indexOf("?") > -1 ){
			var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
			var aQueryString = strQueryString.split("&");
			for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
				if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
					var aParam = aQueryString[iParam].split("=");
					strReturn = aParam[1];
					break;
				}
			}
	  }
 		return strReturn;
	}
</script>
<table cellpadding="0" cellspacing="0" align="center" width="70%">
	<tr bgcolor="#D8DA85">
		<td style="border-bottom:1px solid #000000;">
			<table cellspacing="0" cellpadding="0" width="100%">
				<tr>
					<td><span class="TB2">&nbsp;<?php print $row['MinistryName']; ?></span></td>
					<td valign="middle" align="right">
						<table cellpadding="3" cellspacing="3">
							<tr>
								<td><span class="T1">&nbsp;</span></td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr bgcolor="#FEFFCD">
		<td style="border-bottom:1px solid #000000;">
			<table cellspacing="0" cellpadding="0" width="100%">
				<tr>
					<td><span class="T1">&nbsp;&nbsp;<em>New Link</em></span></td>
					<td valign="middle" align="right">
						<table cellpadding="3" cellspacing="3">
							<tr>
								<td>
									<a class="L0" href="javascript:showHideEdit('New');">New Link</a></td>
							</tr>
						</table>
					</td>
				</tr>
				<tr bgcolor="#FFFFFF">
					<td colspan="2" align="center">
						<div id="BarNew" style="display:none;">
							<div id="ContentNew" style="display:none;">
								<br><form name="FormNew" method="post" action="index.php?action=menu_item_new&div_code=<?php print $_GET['div_code']; ?>">
									<table cellpadding="2" cellspacing="3" style="background:#FEFFCD;border:1px solid #D8DA85;" width="80%" border="0">
										<tr>
											<td valign="middle"><span class="T0">Display&nbsp;</span></td><td valign="middle"><input type="text" name="txtDisplay" class="T1" size="30"></td>
										</tr>
										<tr>
											<td valign="middle"><span class="T0">Web Address&nbsp;</span></td><td><input type="text" name="txtLink" class="T1" size="50"></td></tr>
										<tr>
											<td valign="middle" colspan="2">
												<table cellpadding="0" cellspacing="0">
													<tr>
														<td valign="middle"><span class="T0">LivingWeb&nbsp;</span></td>
														<td valign="top"><?php buildPagesSelect(); ?></td>
													</table></td>
										</td>
										</tr>
										<tr>
											<td align="center" colspan="2"><input name="txtOriginalOrder" value="New" type="hidden"><input type="submit" name="btnSubmit" class="B" value="Save"></td>
										</tr>
									</table></form>
							</div>
						</div>
					</td>
				</tr>
			</table>
		</td>
	</tr>
<?php
	function buildPagesSelect()
	{
		global $DB;
		$DB->query("select * from pages join ministries on pages.MinistryID = ministries.MinistryID order by MinistryName");
		print '<select name="selPages" class="T1" onChange="this.form.txtLink.value = buildURL(this.form.selPages.options[this.form.selPages.selectedIndex].value);">';
		print '<option value = "" default></option>';
		while($row = $DB->fetch_row())
		{
			print '<option value="' . $row['PageID'] . '">' . $row['MinistryName'] . ' - ' . substr($row['PageTitle'],0,10) . ' (PID ' . $row['PageID'] . ')</option>';
		}
		print '</select>';
	
	}

	global $DB;
	$DB->query("SELECT Display,MinistryID,LinkOrder FROM menu WHERE MinistryID = '" . $_GET['div_code'] . "' ORDER BY LinkOrder");
	while($row = $DB->fetch_row())
	{
		print '
		<tr>
			<td style="border-bottom:1px solid #000000;">
				<table cellspacing="0" cellpadding="0" width="100%">
					<tr>
						<td><span class="T1">&nbsp;&nbsp;' . $row['Display'] . '</span></td>
						<td valign="middle" align="right">
							<table cellpadding="3" cellspacing="3">
								<tr>
									<td>
										<a class="L0" href="index.php?action=menu_item_move&div_code=' .  $_GET['div_code'] . '&link_num=' . $row['LinkOrder'] . '&direction=U">Up</a></td>
									<td>
										<span class="T0">|</span></td>
									<td>
										<a class="L0" href="index.php?action=menu_item_move&div_code=' .  $_GET['div_code'] . '&link_num=' . $row['LinkOrder'] . '&direction=D">Down</a></td>
									<td>
										<span class="T0">|</span></td>
									<td>
										<a class="L0" href="javascript:showHideEdit(\'' . $row['LinkOrder'] . '\');">Edit</a></td>
									<td>
										<span class="T0">|</span></td>
									<td>
										<a class="L0" href="javascript:if(confirm(\'Delete this link?\')){location.href=\'index.php?action=menu_by_div&link_num=' . $row['LinkOrder'] . '&div_code=' . $_GET['div_code'] . '\';}">Delete</a></td>
								</tr> 	
							</table>
						</td>
					</tr>
					<tr>
						<td colspan="2" align="center">
							<div id="Bar' . $row['LinkOrder'] . '" style="display:none;">
								<div id="Content' . $row['LinkOrder'] . '" style="display:none;">
									<br><form name="Form' . $row['LinkOrder'] . '" method="post" action="index.php?action=menu_item_edit&div_code=' .  $_GET['div_code'] . '">
									<table cellpadding="2" cellspacing="3" style="background:#FEFFCD;border:1px solid #D8DA85;" width="80%" border="0">
										<tr>
											<td valign="middle"><span class="T1">Display&nbsp;</span></td><td valign="middle"><input type="text" name="txtDisplay" class="T1" size="30"></td>
											<!--<td valign="middle" align="right"><span class="T1">Order&nbsp;</span></td><td valign="middle"><input type="text" name="txtOrder" class="T1" size="2"></td>-->
										</tr>
										<tr>
											<td valign="middle"><span class="T1">Link&nbsp;</span></td><td colspan="3"><input type="text" name="txtLink" class="T1" size="47"></td>
										</tr>
										<tr>
											<td align="center" colspan="4"><input name="txtLinkOrder" type="hidden" value="'. $row['LinkOrder'] .'"><input type="submit" name="btnSubmit" class="B" value="Save"></td>
										</tr>
									</table></form>
								</div>
							</div>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		';
	}
?>
</table>